/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * IFormPeriksa.java
 *
 * Created on 15 Jun 10, 6:33:27
 */
package clinsoftbeta.GUI;

import clinsoftbeta.database.Database;
import com.mysql.jdbc.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Sidiq Prihatno
 */
public class IFormPeriksa extends javax.swing.JInternalFrame {

    DefaultTableModel tbModel;
    String query = "";
    private Statement st;
    private Database db;
    private ResultSet rs;
    String modeOperasi = "";

    /** Creates new form IFormPeriksa */
    public IFormPeriksa() {
        initComponents();
        setVisible(true);
        db = new Database();
        db.koneksiDatabase();
        isikanTabel();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        txtPeriksa = new javax.swing.JTextField();
        txtTglPeriksa = new javax.swing.JTextField();
        txtKodePasien2 = new javax.swing.JTextField();
        txtKodeDokter2 = new javax.swing.JTextField();
        txtSuhuBdn = new javax.swing.JTextField();
        txtTekDarah = new javax.swing.JTextField();
        txtBeratBadan = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        taResep = new javax.swing.JTextArea();
        jLabel11 = new javax.swing.JLabel();
        txtNoDaftar = new javax.swing.JTextField();
        jPanel7 = new javax.swing.JPanel();
        btnHapus = new javax.swing.JButton();
        btnBersih = new javax.swing.JButton();
        btnSimpan = new javax.swing.JButton();
        btnBatal = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        tbPeriksa = new javax.swing.JTable();

        setClosable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Form Pemeriksaan Pasien");

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel6.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jPanel6.setLayout(new java.awt.GridBagLayout());

        jLabel4.setText("No Periksa");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 6;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(21, 10, 21, 10);
        jPanel6.add(jLabel4, gridBagConstraints);

        jLabel5.setText("Tanggal Periksa");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(8, 10, 8, 10);
        jPanel6.add(jLabel5, gridBagConstraints);

        jLabel6.setText("Kode Pasien");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(8, 10, 8, 10);
        jPanel6.add(jLabel6, gridBagConstraints);

        jLabel7.setText("Kode Dokter");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(8, 10, 8, 10);
        jPanel6.add(jLabel7, gridBagConstraints);

        jLabel8.setText("Berat Badan");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(8, 10, 8, 10);
        jPanel6.add(jLabel8, gridBagConstraints);

        jLabel9.setText("Tekanan Darah");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(8, 10, 8, 10);
        jPanel6.add(jLabel9, gridBagConstraints);

        jLabel10.setText("Suhu Tubuh");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(8, 10, 8, 10);
        jPanel6.add(jLabel10, gridBagConstraints);

        jLabel12.setText("Resep Dokter");
        jLabel12.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jLabel12.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(7, 10, 7, 10);
        jPanel6.add(jLabel12, gridBagConstraints);

        txtPeriksa.setPreferredSize(new java.awt.Dimension(50, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 6;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(21, 0, 21, 8);
        jPanel6.add(txtPeriksa, gridBagConstraints);

        txtTglPeriksa.setPreferredSize(new java.awt.Dimension(10, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(8, 10, 8, 8);
        jPanel6.add(txtTglPeriksa, gridBagConstraints);

        txtKodePasien2.setPreferredSize(new java.awt.Dimension(70, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 8;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(8, 10, 8, 8);
        jPanel6.add(txtKodePasien2, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 8;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(8, 10, 8, 8);
        jPanel6.add(txtKodeDokter2, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(8, 10, 8, 10);
        jPanel6.add(txtSuhuBdn, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(8, 10, 8, 10);
        jPanel6.add(txtTekDarah, gridBagConstraints);

        txtBeratBadan.setMinimumSize(new java.awt.Dimension(100, 20));
        txtBeratBadan.setPreferredSize(new java.awt.Dimension(50, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 2;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(8, 10, 8, 10);
        jPanel6.add(txtBeratBadan, gridBagConstraints);

        jLabel1.setText("Kg");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 4;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(8, 10, 8, 10);
        jPanel6.add(jLabel1, gridBagConstraints);

        jLabel2.setText("mmHg");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(8, 10, 8, 10);
        jPanel6.add(jLabel2, gridBagConstraints);

        jLabel3.setText("º C");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(8, 10, 8, 10);
        jPanel6.add(jLabel3, gridBagConstraints);

        taResep.setColumns(20);
        taResep.setRows(5);
        taResep.setPreferredSize(new java.awt.Dimension(164, 50));
        jScrollPane2.setViewportView(taResep);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.gridwidth = 17;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(7, 10, 7, 10);
        jPanel6.add(jScrollPane2, gridBagConstraints);

        jLabel11.setText("No Pendaftaran");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 3;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(22, 9, 22, 10);
        jPanel6.add(jLabel11, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 51;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(22, 10, 22, 11);
        jPanel6.add(txtNoDaftar, gridBagConstraints);

        jPanel4.add(jPanel6);

        jPanel1.add(jPanel4, java.awt.BorderLayout.PAGE_START);

        jPanel7.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 20, 10));

        btnHapus.setText("Hapus");
        btnHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHapusActionPerformed(evt);
            }
        });
        jPanel7.add(btnHapus);

        btnBersih.setText("Bersih");
        btnBersih.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBersihActionPerformed(evt);
            }
        });
        jPanel7.add(btnBersih);

        btnSimpan.setText("Simpan");
        btnSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSimpanActionPerformed(evt);
            }
        });
        jPanel7.add(btnSimpan);

        btnBatal.setText("Batal");
        btnBatal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBatalActionPerformed(evt);
            }
        });
        jPanel7.add(btnBatal);

        jPanel1.add(jPanel7, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.WEST);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jScrollPane4.setAutoscrolls(true);

        tbPeriksa.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null}
            },
            new String [] {
                "No Periksa", "Tgl. Periksa", "Kode Pasien", "Kode Dokter", "Berat Badan", "Tekanan Darah", "Suhu Tubuh", "Resep Dokter"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tbPeriksa.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbPeriksaMouseClicked(evt);
            }
        });
        jScrollPane4.setViewportView(tbPeriksa);

        jPanel2.add(jScrollPane4, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void isikanTabel() {
        String[] tbHeader = {"No. Periksa", "No. Pendaftaran", "Tanggal", "Kode Pasien", "Kode Dokter", "Berat Badan", "Tekanan Darah", "Suhu Tubuh", "Resep"};
        tbModel = new DefaultTableModel(null, tbHeader);
//        kosongkanTabel();
        try {
            query = "Select * from periksa";
            st = (Statement) db.koneksi.createStatement();
            rs = st.executeQuery(query);
            while (rs.next()) {
                String[] tbData = {rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(8)};
                tbModel.addRow(tbData);
            }
        } catch (SQLException se) {
            se.printStackTrace();
            //System.exit(0);
        }
        tbPeriksa.setModel(tbModel);
        if (tbPeriksa.getRowCount() > 0) {
            tbPeriksa.addRowSelectionInterval(0, 0);
            //      tampilkanDiText();
//            aktifText(false);
//            AktifSimpan(false);
        } else {
            //btnBaruActionPerformed(null);
        }
    }
    private void btnBatalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBatalActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_btnBatalActionPerformed

    private void btnSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSimpanActionPerformed
        // TODO add your handling code here:
        if (txtNoDaftar.getText().trim().length() == 0) {
            JOptionPane.showMessageDialog(this, "Data ada yang masih kosong");
        }
            try {
                query = "INSERT INTO periksa VALUES('" + txtNoDaftar.getText() + "','" + txtNoDaftar.getText() + "','" + txtTglPeriksa.getText() + "','" + txtKodePasien2.getText() + "','" + txtKodeDokter2.getText() + "','" + txtBeratBadan.getText() + "','"
                        + txtTekDarah.getText() + "','" + txtSuhuBdn.getText() + "','" + taResep.getText() + "')";
                PreparedStatement presttmt = db.koneksi.prepareStatement(query);
                presttmt.executeUpdate(query);
                JOptionPane.showMessageDialog(null, "Data berhasil disimpan");
                //dispose();
                //tbDaftar.revalidate();
            } catch (SQLException se) {
                JOptionPane.showMessageDialog(null,
                        "Terjadi kesalahan input data! error :" + se,
                        "Kesalahan", JOptionPane.ERROR_MESSAGE);
               // dispose();
            }
        
    }//GEN-LAST:event_btnSimpanActionPerformed

    private void btnHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHapusActionPerformed
        // TODO add your handling code here:
        if (txtPeriksa.getText().trim().length() == 0) {
            JOptionPane.showMessageDialog(this, "Data ada yang masih kosong");
        }
        try {
            query = "delete from periksa where no_periksa ='" + txtPeriksa.getText() + "'";
            PreparedStatement presttmt = db.koneksi.prepareStatement(query);
            presttmt.executeUpdate(query);
            JOptionPane.showMessageDialog(null, "Data berhasil di hapus");
            //dispose();
            //tbDaftar.revalidate();
        } catch (SQLException se) {
            JOptionPane.showMessageDialog(null,
                    "Terjadi kesalahan hapus data! error :" + se,
                    "Kesalahan", JOptionPane.ERROR_MESSAGE);
            dispose();
        }
    
    }//GEN-LAST:event_btnHapusActionPerformed

    private void tbPeriksaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbPeriksaMouseClicked
        // TODO add your handling code here:
        if (evt.getClickCount() == 1) {
            Tampil();


}
    }
    int row = 0;

    public void

Tampil() {
        row = tbPeriksa.getSelectedRow();
        txtPeriksa.

setText(tbPeriksa.getValueAt(row, 0).toString());
        txtNoDaftar.

setText(tbPeriksa.getValueAt(row, 1).toString());
        txtTglPeriksa.

setText(tbPeriksa.getValueAt(row, 2).toString());
        txtKodePasien2.

setText(tbPeriksa.getValueAt(row, 3).toString());
        txtKodeDokter2.

setText(tbPeriksa.getValueAt(row, 4).toString());
        txtBeratBadan.

setText(tbPeriksa.getValueAt(row, 5).toString());
        txtTekDarah.

setText(tbPeriksa.getValueAt(row, 6).toString());
        txtSuhuBdn.

setText(tbPeriksa.getValueAt(row, 7).toString());
        taResep.

setText(tbPeriksa.getValueAt(row, 8).toString());
    }//GEN-LAST:event_tbPeriksaMouseClicked

    private void btnBersihActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBersihActionPerformed
        // TODO add your handling code here:
        txtPeriksa.setText("");
        txtNoDaftar.

setText("");
        txtTglPeriksa.

setText("");
        txtKodeDokter2.

setText("");
        txtKodePasien2.

setText("");
        txtBeratBadan.

setText("");
        txtTekDarah.

setText("");
        txtSuhuBdn.

setText("");
        taResep.

setText("");
    }//GEN-LAST:event_btnBersihActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBatal;
    private javax.swing.JButton btnBersih;
    private javax.swing.JButton btnHapus;
    private javax.swing.JButton btnSimpan;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTextArea taResep;
    private javax.swing.JTable tbPeriksa;
    private javax.swing.JTextField txtBeratBadan;
    private javax.swing.JTextField txtKodeDokter2;
    private javax.swing.JTextField txtKodePasien2;
    private javax.swing.JTextField txtNoDaftar;
    private javax.swing.JTextField txtPeriksa;
    private javax.swing.JTextField txtSuhuBdn;
    private javax.swing.JTextField txtTekDarah;
    private javax.swing.JTextField txtTglPeriksa;
    // End of variables declaration//GEN-END:variables

}
